package com.example.demo.controller;

import com.example.demo.aop.MyLog;
import com.example.demo.converter.UserConverter;
import com.example.demo.dto.PageRequest;
import com.example.demo.dto.Result;
import com.example.demo.dto.UserDto;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @MyLog
    @GetMapping("/users/{id}")
    public Result<UserDto> findById(@PathVariable Integer id) {
        User byId = this.userService.findById(id);
        // mapstruct
        // beancopier
        // beanpro
        return Result.success(UserConverter.C.toDto(byId));
    }

    @PostMapping(value = "/users")
    public UserDto save(@RequestBody @Validated UserDto u) {
        return u;
    }

    @GetMapping("/search")
    public List<UserDto> search(PageRequest<UserDto> req) {
        return this.userService.search(req);
    }

    public void test() {
        this.userService.doBiz();
    }
}
